package com.ruoyi.interview.service.impl;

import com.ruoyi.interview.domain.Info;
import com.ruoyi.interview.mapper.InfoMapper;
import com.ruoyi.interview.service.IInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class InfoServiceImpl implements IInfoService {

    @Resource
    private InfoMapper infoMapper;

    private final Random random = new Random();

    @Override
    public String randomInsertInfoBatch(Integer createNum) {
        List<Info> infoList = new ArrayList<>(createNum);
        for (int i = 0; i < createNum; i++) {
//            Random random = new Random();
//            (max - min + 1) + min
//            1000 - 9999 之间的随机数
//            int name = random.nextInt(9999 - 1000 + 1) + 1000;
            String name = getRandomFour();
//            10 - 80随机数 生成年龄
            int age = random.nextInt(80 - 10 + 1) + 10;
            Info info = new Info(name, age);
            infoList.add(info);
        }
        long startTime = System.currentTimeMillis();
        int i = infoMapper.insertInfoBatch(infoList);
        long endTime = System.currentTimeMillis() - startTime;
        return "当前新增" + i + "条数据,一共耗时" + endTime / 1000 + "秒";
    }
    /**
     * 随机四位英语字母
     */
    private String getRandomFour(){
//        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int randomNum = random.nextInt(122 - 97 + 1) + 97;
            char c = (char) randomNum;
            sb.append(c);
        }
        return sb.toString();
    }
}
